<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
    <el-col>
      <strong v-if="isRoot">{{ allData.name }}</strong>
      <span v-else>{{ allData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ allData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handledropdowm">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/department'
export default {
  props: {
    allData: {
      type: Object,
      required: true,
      default() {
        return {}
      }
    },
    isRoot: {
      type: Boolean,
      defalut: false
    }
  },
  methods: {
    async handledropdowm(value) {
      if (value === 'add') {
        // 添加子部门
        this.$emit('addDepartment', this.allData)
      } else if (value === 'del') {
        try {
        // 删除子部门
          await this.$confirm('是否确认删除该部门', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          await delDepartment(this.allData.id)
          this.$emit('delDepartment')
          this.$message.success('部门删除成功')
        } catch (err) {
          console.log(err)
        }
      } else if (value === 'edit') {
        // 编辑子部门
        this.$emit('editDepartment', this.allData)
      }
    }
  }
}
</script>

<style>

</style>
